package com.netease.examination.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * designed by destiny
 *
 * AOP实现切面日志
 * 用于打印所有的controller层放的参数
 *
 * @author destiny
 *         e-mail destinywk@163.com
 *         github https://github.com/DestinyWang
 *         oschina https://git.oschina.net/destinywk
 * @version JDK 1.8.0_101
 * @since 2017/3/15 16:59
 */
@Aspect
@Component
public class LogAspect {

    private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Before("execution(* com.netease.examination.web.*.*Controller.*(..))")
    public void beforeMethod(JoinPoint joinPoint) {
        StringBuilder stringBuilder = new StringBuilder("|--- ");
        for (Object arg : joinPoint.getArgs()) {
            stringBuilder.append(arg).append(" + ");
        }
        stringBuilder.replace(stringBuilder.length() - 2, stringBuilder.length(), "---|");

//        logger.info("before method : " + stringBuilder.substring(0, stringBuilder.length() - 2));
        logger.info("before method : " + stringBuilder.toString());
    }

    @After("execution(* com.netease.examination.web.*.*Controller.*(..))")
    public void afterMethod(JoinPoint joinPoint) {
        StringBuilder stringBuilder = new StringBuilder("|--- ");
        for (Object arg : joinPoint.getArgs()) {
            stringBuilder.append(arg).append(" + ");
        }
        stringBuilder.replace(stringBuilder.length() - 2, stringBuilder.length(), "---|");

//        logger.info("after method : " + stringBuilder.substring(0, stringBuilder.length() - 2));
        logger.info("after method : " + stringBuilder.toString());
    }

}
